1
'****************************** Module Header ******************************'
2 ' Module Name: One2ManyClass.vb
3 ' Project: VBEFEntityDataModel
4 ' Copyright (c) Microsoft Corporation.
6 ' This example illustrates how to dinsert, update and query the two entities
7 ' with one to many association.
9 ' This source is subject to the Microsoft Public License.
10 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 ' All other rights reserved.
13 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 '***************************************************************************'
18 #Region
"Imports directives"
20 Imports System
.Collections
.Generic
26 Namespace VBEFEntityDataModel
.One2Many
27 Friend Class One2ManyClass
29 ' Test all the methods in One2ManyClass
30 Public Shared
Sub One2ManyTest()
31 InsertDepartmentWithCourse()
41 ' Insert new department with new course
42 Public Shared
Sub InsertDepartmentWithCourse()
44 Using context
As New EFO2MEntities()
46 Dim department
As New Department() With _
49 .Name
= "Software Engineering" _
52 Dim course
As New Course() With _
58 department
.Course
.Add(course
)
60 context
.AddToDepartment(department
)
64 Console
.WriteLine("Inserting department {0} with course " _
65 + "{1}", department
.Name
, course
.Title
)
72 Console
.WriteLine(ex
.Message
)
78 ' Insert new course to existing department
79 Public Shared
Sub InsertCourse()
81 Using context
As New EFO2MEntities()
82 Dim course
As New Course() With _
85 .Title
= "Object Oriented Programming" _
88 course
.Department
= (From p
In context
.Department _
89 Where p
.DepartmentID
= 7 _
92 context
.AddToCourse(course
)
96 Console
.WriteLine("Inserting course {0} to department " _
97 + "{1}", course
.Title
, course
.Department
.Name
)
103 Catch ex
As Exception
104 Console
.WriteLine(ex
.Message
)
110 ' Get all the departments with courses
111 Public Shared
Sub Query()
113 Using context
As New EFO2MEntities()
115 Dim query
= From p
In context
.Department
.Include("Course") _
118 Console
.WriteLine("Deparments with their courses:")
120 For Each d
As Department
In query
122 Console
.WriteLine("{0} {1}", d
.DepartmentID
, d
.Name
)
124 For Each c
As Course
In d
.Course
126 Console
.WriteLine(" {0}", c
.Title
)
137 ' Update one existing course
138 Public Shared
Sub UpdateCourse()
140 Using context
As New EFO2MEntities()
142 Dim course
As New Course()
144 course
.CourseID
= 2203
146 context
.AttachTo("Course", course
)
152 Console
.WriteLine("Modifying Course 2203's Title to {0}", _
155 context
.SaveChanges()
159 Catch ex
As Exception
160 Console
.WriteLine(ex
.Message
)
166 ' Update one existing department
167 Public Shared
Sub UpdateDepartment()
169 Using context
As New EFO2MEntities()
171 Dim department
As New Department()
173 department
.DepartmentID
= 1
175 context
.AttachTo("Department", department
)
177 department
.Name
= "Computer Engineering"
179 department
.Course
.Add(New Course() With _
182 .Title
= "Arithmetic" _
187 Console
.WriteLine("Modifying Department 1's Title to {0}" _
188 + ", and insert a new Course 2204 into the " + _
189 "Department 1", department
.Name
)
191 context
.SaveChanges()
195 Catch ex
As Exception
196 Console
.WriteLine(ex
.Message
)